﻿namespace Web.Control.WWW.Group
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using Model.WWW_WWW.Group;
    using System.Collections.Generic;

    /// <summary>
    /// EditGroup 的摘要说明
    /// </summary>
    public class Topic : BaseControl
    {
        /// <summary>
        /// 讨论话题ID
        /// </summary>
        protected string tid = "";        
        /// <summary>
        /// 讨论信息
        /// </summary>
        protected TopicModel tm;
        /// <summary>
        /// 当前会员编号
        /// </summary>
        protected string Memberno = "";
        /// <summary>
        /// 绑定讨论评论数据
        /// </summary>
        protected Repeater rptReTopic;
        /// <summary>
        /// 绑定本小组下的最新5个讨论数据
        /// </summary>
        protected Repeater rptTopic;
        protected int PageIndex=1;
        protected Literal lRTCount;
        protected override void OnLoad(EventArgs e)
        {
            Memberno = HttpContext.Current.User.Identity.Name;            
            if (!string.IsNullOrEmpty(Request["tid"]))
            {
                tid = Request["tid"];
            }
            else
            {
                Response.End();
                return;
            }
            int psize = 15;
            if (lRTCount != null) int.TryParse(lRTCount.Text, out psize);
            if (!string.IsNullOrEmpty(Request["p"]))
            {
                if (Request["page"] == "L")
                {
                    PageIndex = 999;
                }
                else
                {
                    int.TryParse(Request["p"], out PageIndex);
                    if (PageIndex == 0)
                    {
                        PageIndex = 1;
                    }
                }
            }
            string sessionID = Session.SessionID;
            string iP = Request.UserHostAddress;
            string userAgent = Request.UserAgent;
            string referer = HttpContext.Current.Request.UrlReferrer == null ? "" : HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
            ArrayList arrList = BLF.WWW_WWW.Group.Group.GetATopic(tid, Memberno, sessionID, iP, userAgent, referer, PageIndex, psize);            
            if (arrList != null && arrList.Count > 0)
            {                
                List<TopicReplyModel> cList = arrList[1] as List<TopicReplyModel>;
                List<TopicReplyModel> pList = arrList[3] as List<TopicReplyModel>;
                BLF.WWW_WWW.Group.Group.ParentContent(cList, pList);
                tm = arrList[0] as TopicModel;
                int pCount = (int)Math.Ceiling(((decimal)tm.Topics) / ((decimal)psize));
                if (PageIndex > pCount) PageIndex = pCount;
                if (rptReTopic != null)
                {
                    rptReTopic.DataSource = cList;
                    rptReTopic.DataBind();
                }
                if (rptTopic != null)
                {
                    rptTopic.DataSource = arrList[2];
                    rptTopic.DataBind();
                }
                GroupModel gModel = arrList[4] as GroupModel;
                //标题
                this.DynamicTitle = GetText("GP525", new string[] { tm.Title, tm.GroTitle });
                if (gModel.Language1 != "")
                {
                    this.DynamicKeywords = GetText("GP526", new string[] { BLF.StaticData.GetLanguageText(gModel.Language1), BLF.StaticData.GetLanguageText(gModel.Language2) });
                }
                else
                {
                    this.DynamicKeywords = BLF.StaticData.GetLanguageText(gModel.Language2);
                }
                this.DynamicDescription = GetText("GP527", new string[] { tm.GroTitle, tm.Title });
            }
            else
            {
                //Response.End();
            }
            base.OnLoad(e);
        }        
    }
}
